package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.RewardtaskExample;
import com.campusify.pojo.RewardtaskExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.RewardtaskMapper;
import com.campusify.pojo.Rewardtask;
import com.campusify.service.RewardTaskService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class RewardTaskServiceImpl implements RewardTaskService {

	@Autowired
	private RewardtaskMapper rewardTaskMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Rewardtask> findAll() {
		return rewardTaskMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Rewardtask> page=   (Page<Rewardtask>) rewardTaskMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Rewardtask rewardTask) {
		rewardTaskMapper.insert(rewardTask);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Rewardtask rewardTask){
		rewardTaskMapper.updateByPrimaryKey(rewardTask);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Rewardtask findOne(Integer taskID){
		return rewardTaskMapper.selectByPrimaryKey(taskID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] taskIDs) {
		for(Integer taskID:taskIDs){
			rewardTaskMapper.deleteByPrimaryKey(taskID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Rewardtask rewardTask, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		RewardtaskExample example=new RewardtaskExample();
		Criteria criteria = example.createCriteria();
		
		if(rewardTask!=null){			
						if(rewardTask.getDescription()!=null && rewardTask.getDescription().length()>0){
				criteria.andDescriptionLike("%"+rewardTask.getDescription()+"%");
			}			if(rewardTask.getTime()!=null && rewardTask.getTime().length()>0){
				criteria.andTimeLike("%"+rewardTask.getTime()+"%");
			}	
		}
		
		Page<Rewardtask> page= (Page<Rewardtask>)rewardTaskMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
